<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.searchType" type="card" @tab-click="switchType">
      <el-tab-pane label="我发起的" name="1"></el-tab-pane>
      <el-tab-pane name="2">
        <template #label>
          <div class="tabs-item">
            <span class="label">待处理</span>
            <span class="badge" v-if="approvalBoard.waitNum">{{approvalBoard.waitNum}}</span>
          </div>
        </template>
      </el-tab-pane>
      <el-tab-pane label="已处理" name="3"></el-tab-pane>
      <el-tab-pane label="我收到的" name="4"></el-tab-pane>
    </el-tabs>

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="procName">
        <el-input
            v-model="queryParams.procName"
            placeholder="请输入标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus">
          <el-option label="全部" :value="''"/>
          <el-option label="审批中" :value="'1'"/>
          <el-option label="挂起" :value="'2'"/>
          <el-option label="撤消" :value="'3'"/>
          <el-option label="通过" :value="'4'"/>
          <el-option label="驳回" :value="'5'"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="instList">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="审批编号" align="center" prop="procInstId" />
      <el-table-column label="标题" align="center" prop="procName" />
      <el-table-column label="流程状态" align="center" prop="approvalStatus" >
        <template #default="scope">
          <dict-tag :options="bpm_approval_status" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180" />
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="showProcInstInfo(scope.row.procInstId)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <ProcInstInfo ref="procInstInfoRef" @close="closeProcInstInfo"/>
  </div>
</template>

<script setup name="Approve">
import ProcInstInfo from "../components/ProcInstInfo.vue";
import { myApproveList, getApprovalBoard } from "@/api/bpm/inst";
import {getCurrentInstance, nextTick, onMounted, reactive, ref, toRefs, watch} from "vue";

const { proxy } = getCurrentInstance();

const { bpm_approval_status } = proxy.useDict("bpm_approval_status");

const instList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const procInstInfoRef = ref(null);
const approvalBoard = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    searchType: '1',
    approvalStatus: "",
    procName: null,
  },
});

const { queryParams, form, rules } = toRefs(data);

onMounted(async () => {
  init()
});

watch(
    () => proxy.$route.query.searchType,
    (val) => {
      init()
    }
);

function init() {
  let searchType = proxy.$route.query.searchType
  if (searchType) {
    queryParams.value.searchType = searchType
  }
  getList()
  getBoard()
}

/**
 * 切换状态
 * @param tab
 * @param event
 */
function switchType(tab, event) {
  nextTick(()=> {
    handleQuery()
  })
}

/** 查询流程实例列表 */
function getList() {
  loading.value = true;
  myApproveList(queryParams.value).then(response => {
    instList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
  getBoard()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function showProcInstInfo(procInstId) {
  procInstInfoRef.value.show(procInstId)
}
function closeProcInstInfo() {
  getList()
  getBoard()
}

function getBoard() {
  getApprovalBoard().then(response => {
    approvalBoard.value = response.data;
  });
}
</script>
<style lang="scss" scoped>
.tabs-item {
  width: 100%;
  height: 100%;
  text-align: center;
  .label {
    font-size: 14px;
  }
  .badge {
    padding: 3px 5px;
    background-color: #F56C6C;
    border-radius: 10px;
    font-size: 10px;
    color: #FFFFFF;
    margin-left: 5px;
  }
}

</style>